package com.himore.api.web.controller.modules;

import java.util.List;

import com.himore.api.common.annotation.Log;
import com.himore.api.common.core.controller.BaseController;
import com.himore.api.common.core.domain.AjaxResult;
import com.himore.api.common.core.domain.entity.SysUser;
import com.himore.api.common.core.page.TableDataInfo;
import com.himore.api.common.enums.BusinessType;
import com.himore.api.common.utils.ErrorCodeUtil;
import com.himore.api.common.utils.SecurityUtils;
import com.himore.api.common.utils.poi.ExcelUtil;
import com.himore.api.system.service.ISysUserService;
import com.himore.api.web.enums.AwardsTypeEnum;
import com.himore.api.web.model.TSelectedAwards;
import com.himore.api.web.model.TSysGifts;
import com.himore.api.web.service.ITSelectedAwardsService;
import com.himore.api.web.service.ITSysGiftsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 礼品Controller
 * 
 * @author ruoyi
 * @date 2021-01-14
 */
@RestController
@RequestMapping("/system/gifts")
public class TSysGiftsController extends BaseController
{
    @Autowired
    private ITSysGiftsService tSysGiftsService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ITSelectedAwardsService tSelectedAwardsService;
    /**
     * 查询礼品列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TSysGifts tSysGifts){
        startPage();
        List<TSysGifts> list = tSysGiftsService.selectTSysGiftsList(tSysGifts);
        return getDataTable(list);
    }

    /**
     * 导出礼品列表
     */
    @Log(title = "礼品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TSysGifts tSysGifts){
        List<TSysGifts> list = tSysGiftsService.selectTSysGiftsList(tSysGifts);
        ExcelUtil<TSysGifts> util = new ExcelUtil<TSysGifts>(TSysGifts.class);
        return util.exportExcel(list, "gifts");
    }

    /**
     * 获取礼品详细信息
     */
    @GetMapping(value = "/{giftId}")
    public AjaxResult getInfo(@PathVariable("giftId") Long giftId){
        return AjaxResult.success(tSysGiftsService.selectTSysGiftsById(giftId));
    }

    /**
     * 新增礼品
     */
    @Log(title = "礼品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TSysGifts tSysGifts){
        return toAjax(tSysGiftsService.insertTSysGifts(tSysGifts));
    }

    /**
     * 修改礼品
     */
    @Log(title = "礼品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TSysGifts tSysGifts){
        return toAjax(tSysGiftsService.updateTSysGifts(tSysGifts));
    }

    /**
     * 删除礼品
     */
    @Log(title = "礼品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{giftIds}")
    public AjaxResult remove(@PathVariable Long[] giftIds){
        return toAjax(tSysGiftsService.deleteTSysGiftsByIds(giftIds));
    }



    /**
     * 新增礼品
     */
    @Log(title = "礼品兑换", businessType = BusinessType.INSERT)
    @PostMapping(value = "/exchange")
    public AjaxResult exchange(@RequestParam Long  giftId){
        try {
            SysUser sysUser = SecurityUtils.getLoginUser().getUser();
            SysUser currentUser =userService.selectUserById(sysUser.getUserId());

            int score = currentUser.getScore();
            TSysGifts tSysGifts = tSysGiftsService.selectTSysGiftsById(giftId);
            if(tSysGifts.getInventory()<=0){
                return AjaxResult.error(ErrorCodeUtil.E_230,"该礼品库存不足，不能兑换该商品！");
            }
            if(score < tSysGifts.getPrice()){
                return AjaxResult.error(ErrorCodeUtil.E_229,"您的积分不足，不能兑换该商品！");
            }
            //将库存减一
            TSysGifts  gifts = new TSysGifts();
            gifts.setGiftId(giftId);
            Long inv = tSysGifts.getInventory() -1;
            gifts.setInventory(inv);
            tSysGiftsService.updateTSysGifts(gifts);

            //个人积分需要减去消费的积分
            SysUser user = new SysUser();
            user.setUserId(sysUser.getUserId());
            user.setScore(0-tSysGifts.getPrice());
            userService.updateScoreOrDrawInfo(user);

            //往我的奖品中 插入一条数据
            TSelectedAwards tSelectedAwards = new TSelectedAwards();
            tSelectedAwards.setAwardType(AwardsTypeEnum.EXCHANGE.getValue());
            tSelectedAwards.setAwardName(tSysGifts.getGiftName());
            tSelectedAwards.setAwardId(tSysGifts.getGiftId());
            tSelectedAwards.setUserId(currentUser.getUserId());
            tSelectedAwardsService.insertTSelectedAwards(tSelectedAwards);

            return AjaxResult.success();
        }catch (Exception e){
            return  AjaxResult.error(ErrorCodeUtil.E_500,"兑换礼品异常，请联系管理员！");
        }



    }



}
